/**
 * Additional content section / block functions for body.
 */

var rootpath = process.cwd() + '/',
    path = require('path'),
    calipso = require(path.join(rootpath, 'lib/calipso')),
    Query = calipso.lib.mongoose.Query;

exports = module.exports = function(req,options,callback) {

//    var hasPermission = false;
//    if (req.session.user && req.session.user.roles) {
//        var roleCount = req.session.user.roles.length;
//        var i;
//        for (i=0; i<roleCount; i++) {
//            var role = req.session.user.roles[i];
//            if (role === 'Member') {
//                hasPermission = true;
//            }
//        }
//    }
//
//    if (hasPermission == false) {
//        callback(null,{ hasPermission:false });
//        return;
//    }

    /**
     *  Get additional content for blocks in the template
     */
    calipso.lib.step(
        function getContentList() {

            // Create a query and retrieve the content, has pager support, using req.moduleParams
            // But you can override on an individual query by setting in the options (second param)

            var condition = {'contentType':'Worship'};

            if(options.user.isAdmin == false) {
                condition.status = 'published';
            }

            if (req.query.type === '1') {
                condition.worshipType = '주일예배';
            } else if (req.query.type === '2') {
                condition.worshipType = '주일오전예배';
            } else if (req.query.type === '3') {
                condition.worshipType = '수요예배';
            } else if (req.query.type === '4') {
                condition.worshipType = '아침예배';
            }

            var query = new Query(condition);

            options.getContentList(query,{req:req,sortBy:'published,desc',pager:true,limit:4},this.parallel());

        },
        function done(err, output) {

            callback(err,{ hasPermission:true, type:req.query.type, contents:output.contents, pager:output.pager });

        }
    );

};